﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using OgilvyOne.BLL;
using OgilvyOne.Model;
using System.Collections;
using ECCIT;
using ECCIT.Web;
using OgilvyOne.UKNow.Model;
using OgilvyOne.UKNow.BLL;
using System.Data;
using System.IO;
public partial class admin_UKNOW_sponsor_sponsor_add : AdminBasePage
{
    private const string VIEWSTATE_COLUMN_ID = "columnid";
    private const string VIEWSTATE_LAYER_ID = "layerid";
    private const string VIEWSTATE_SPONSOR_ID = "sid";
    private const string VIEWSTATE_LOGO_IMG = "logoImage";//
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            ViewState[VIEWSTATE_COLUMN_ID] = int.Parse(Request.Params[VIEWSTATE_COLUMN_ID]);
            ViewState[VIEWSTATE_LAYER_ID] = Request.Params[VIEWSTATE_LAYER_ID];

            string sSponsorID = RequestContext.GetParamValueByParamName(VIEWSTATE_SPONSOR_ID, "");
            ViewState[VIEWSTATE_SPONSOR_ID] = sSponsorID;
            ViewState[VIEWSTATE_LOGO_IMG] = "";//
            if (string.IsNullOrEmpty(sSponsorID))
                InitPage(null, 0);
            else
                InitPage(int.Parse(sSponsorID), 0);
        }
    }
        private void InitPage(int? sSponsorID, int type)
    {
        InitControls();

        SponsorModel model = null;
        if (sSponsorID.HasValue)
        {
            SponsorBLL bll = new SponsorBLL(this.LangID);
            model = bll.selSponsor(sSponsorID.Value);
        }

        BindSponsorToControls(model, type);
    }
        private void BindSponsorToControls(SponsorModel info, int type)
        {
            if (info == null)
            {
                infoname.Text = "新增赞助商";
                return;
            }
            infoname.Text = "修改赞助商";
            name.Text = info.Name;
            sponsorDetail.Text = info.Detail;
            pub.SelectedValue = info.State.ToString();     
            templateId.SelectedValue = (info.TemplateID.HasValue) ? info.TemplateID.Value.ToString() : "";
            HtmlPath.Text = info.HtmlFilePath;
            //将Logo图片路径保存在ViewState里面，用于在没有修改Logo图片的情况下保存数据的时候从ViewState里面将原来的图片路径取出来保存到数据库
            ViewState[VIEWSTATE_LOGO_IMG] = info.LogoPath;
        }
        private void InitControls()
        {
            //初始模板
            this.templateId.Items.Clear();
            ModelManageBLL mmb = new ModelManageBLL();
            DataSet temdata = mmb.GetModel("");
            for (int i = 0; i < temdata.Tables.Count; i++)
            {
                DataTable table = temdata.Tables[i];
                for (int j = 0; j < table.Rows.Count; j++)
                {
                    DataRow row = table.Rows[j];

                    this.templateId.Items.Add(new ListItem(row["tem_name"].ToString(), row["tem_id"].ToString()));

                }
            }
        }
        protected void Sub_click(object sender, EventArgs e)
        {
            SponsorBLL bll = new SponsorBLL(this.LangID);
            SponsorModel info = new SponsorModel();

            info.ID = null;
            if (!string.IsNullOrEmpty(ViewState[VIEWSTATE_SPONSOR_ID].ToString()))
                info.ID = int.Parse(ViewState[VIEWSTATE_SPONSOR_ID].ToString());
            info.Name = name.Text;
            info.Detail = sponsorDetail.Text;
            info.LanguageID = LangID;
            info.State = int.Parse(pub.SelectedValue);
            info.HtmlFilePath = HtmlPath.Text;
            info.ColumnID = int.Parse(ViewState[VIEWSTATE_COLUMN_ID].ToString());
            info.LayerIDs = ViewState[VIEWSTATE_LAYER_ID].ToString();
            info.TemplateID = int.Parse(templateId.SelectedValue);
            info.InsertedTime = DateTime.Now;
            info.UpdatedTime = DateTime.Now;

            string filename = "";
            string path = Server.MapPath("~/uploadfiles/image/");
            //Logo图上传
            if (file.PostedFile.ContentLength > 0)
            {
                string logoname = Guid.NewGuid().ToString();
                string ext = file.FileName.Substring(file.FileName.LastIndexOf("."), file.FileName.Length - (file.FileName.LastIndexOf(".")));
                filename = logoname + ext;
                if (File.Exists(path + filename))
                {
                    File.Delete(path + filename);
                }
                else
                {
                    file.SaveAs(path + filename);
                }
            }
            
            info.LogoPath = string.IsNullOrEmpty(filename) ? ViewState[VIEWSTATE_LOGO_IMG].ToString() : filename;
           
            ResultInfo res = bll.Save(info);
            if (res.Result)
            {
                Response.Redirect("sponsor_list.aspx?columnid=" + ViewState[VIEWSTATE_COLUMN_ID] + "&layerid=" + ViewState[VIEWSTATE_LAYER_ID] + "&langID=" + LangID);
            }
            else
            {
                Response.Write("<script>alert('保存失败')</script>");
                Response.Write(res.Message);
            }

        }
        protected void lbBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("sponsor_list.aspx?columnid=" + ViewState[VIEWSTATE_COLUMN_ID] + "&layerid=" + ViewState[VIEWSTATE_LAYER_ID] + "&langID=" + LangID);
        }
}